/**   
* @Title: CommentAdminController.java 
* @Package com.thomas.controller.admin 
* @Description: TODO(用一句话描述该文件做什么) 
* @author liujian   
* @date 2017年9月6日 下午5:40:45 
* @version V1.0   
*/
package com.thomas.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thomas.entity.Comment;
import com.thomas.entity.PageBean;
import com.thomas.service.CommentService;
import com.thomas.util.DateJsonValueProcessor;
import com.thomas.util.ResponseUtil;

/** 
 * @ClassName: CommentAdminController 
 * @Description: 管理员评论Controller层 
 * @author thomas_liu
 * @date 2017年9月6日 下午5:40:45 
 *  
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentAdminController {
	
	@Autowired
	private CommentService commentService;
	
	/**
	 *分页查询评论信息
	 *@param page
	 *@param rows
	 *@param response
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,@RequestParam(value="state",required=false)String state,HttpServletResponse response)throws Exception{
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("state", state);//评论状态
		List<Comment> commentList=commentService.list(map); 
		Long total=commentService.getTotal(map);
		JSONObject result= new JSONObject();
		JsonConfig jsonConfig= new JsonConfig(); 
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray= JSONArray.fromObject(commentList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *删除评论信息
	 *@param ids
	 *@param response
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String[] idsStr = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			commentService.delete(Integer.parseInt(idsStr[i]));
		}
		JSONObject result= new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *评论审核 
	 *@param comment
	 *@param response
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/review")
	public String review(@RequestParam(value="ids")String ids,@RequestParam(value="state")Integer state,HttpServletResponse response)throws Exception{
		String[] idsStr = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			Comment comment= new Comment();
			comment.setState(state);
			comment.setId(Integer.parseInt(idsStr[i]));
			commentService.update(comment);
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
}
